package com.hfengxiang.example.canvasrotatedemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

public class AnimateView extends View {
    private Paint paint;
    private Rect renderRect;
    private Bitmap bitmap;
    private float rotation;

    public AnimateView setRotationAngle(float rotation) {
        this.rotation = rotation;
        return this;
    }

    public AnimateView setRenderRect(Rect renderRect) {
        this.renderRect = renderRect;
        return this;
    }

    public AnimateView setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
        return this;
    }

    public AnimateView(Context context) {
        super(context);
        paint = new Paint();
        paint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(bitmap!=null&&!bitmap.isRecycled()&&renderRect!=null){
            canvas.drawBitmap(bitmap,null,renderRect,paint);
            canvas.rotate(rotation,renderRect.centerX(),renderRect.centerY());
        }
    }
}
